package com.example.admin.service.impl;

import cn.hutool.crypto.SecureUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.admin.mapper.AdminMapper;
import com.example.admin.service.AdminService;
import com.example.xo.common.pojo.Admin;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author liguoping
 */
@Slf4j
@Service
public class AdminServiceImpl implements AdminService {
    @Autowired
    public AdminMapper adminMapper;
    @Override
    public boolean login(Admin admin) {
        Admin adminInDb =findByEmail(admin.getEmail());

        //数据库中找到了一个adminInDb
        if (adminInDb == null) {
            //账号邮箱错了
            return false;
        }
        //能走这里说明，上面找到了一个非空的adminInDb
        //接下来就要比较密码，adminInDb带的密码是密文
        //所以要把参数admin中的密码转成密文才能比较
        String s = SecureUtil.md5(admin.getPassword());
        if(!s.equals(adminInDb.getPassword())){
            //密码错了
            return false;
        }
        return true;
    }

    @Override
    public Admin findByEmail(String email) {
        LambdaQueryWrapper<Admin> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(Admin::getEmail,email);
        wrapper.isNull(Admin::getDeleted);

        Admin admin = adminMapper.selectOne(wrapper);
        return admin;
    }
}
